package com.shangdiao.sd_server.server.controller;

import java.io.File;
import java.util.Date;
import java.util.Iterator;
import java.util.Random;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.propertyeditors.StringTrimmerEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;

import com.alibaba.fastjson.JSONObject;
import com.shangdiao.sd_server.constant.Constant;
import com.shangdiao.sd_server.constant.ErrorCode;
import com.shangdiao.sd_server.pojo.ResponseMsg;
import com.shangdiao.sd_server.util.DateUtil;
import com.shangdiao.sd_server.util.JsonUtil;


@Controller
@RequestMapping( "/open" )
public class FileController {

    
    @InitBinder     /* Converts empty strings into null when a form is submitted */
    public void initBinder(WebDataBinder binder) {
        binder.registerCustomEditor(String.class, new StringTrimmerEditor(true));
    }
    
    
    @RequestMapping(value = "/fileUpload.json",produces="application/json;charset=utf-8", method = RequestMethod.POST)  
    public @ResponseBody ResponseMsg fileUpload(HttpServletRequest request)   {  
    	ResponseMsg retMsg = null;
    		int max_file_size = 1024*1024*10; //10M
	    	try {
	    		System.out.println("11" + DateUtil.DateToString(new Date(), DateUtil.FORMAT_ONE));
	 	        // 设置上下方文  
		        CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(  
		                request.getSession().getServletContext());  

		        // 检查form是否有enctype="multipart/form-data"  
		        System.out.println("multipartResolver.isMultipart(request)" + multipartResolver.isMultipart(request));
		        if (multipartResolver.isMultipart(request)) {  
		            MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request;  
		            
		            
		            Iterator<String> iter = multiRequest.getFileNames();  
		            
		            MultipartFile file = multiRequest.getFile("file"); 
		            System.out.println("file1 >>> >>> " + JsonUtil.objectToJsonStr(file));
		            while (iter.hasNext()) {  
		                // 由CommonsMultipartFile继承而来,拥有上面的方法.  
		                file = multiRequest.getFile(iter.next());  
		                System.out.println("file2 >>> >>> " + JsonUtil.objectToJsonStr(file));
		                if (file != null) {  
		                	if (file.getSize() > max_file_size) {
		    		 			retMsg =   new ResponseMsg("文件不能大于"+max_file_size/1024+"KB",ErrorCode.ERROR,null);
		    					return retMsg;		
							}else{
								Random random = new Random();
								int next = random.nextInt(100000);
								String fileName = System.currentTimeMillis() + "_" + next + ".png";
								System.out.println("fileName >>> >>> " + fileName);
			                	File localFile = new File(Constant.TEMP_FILE_PATH + fileName);//原图 
			                    File parent = localFile.getParentFile(); 
			                    if(parent!=null&&!parent.exists()){ 
			                    	parent.mkdirs(); 
			                    } 
			                    //上传文件
			                    file.transferTo(localFile);
			                    String url = Constant.DOMAIN + Constant.TEMP_FILE_URL + fileName;
			        			retMsg =   new ResponseMsg("Ok",ErrorCode.OK, url);
			        			
			        			return retMsg;	
							}
		            		                    
		                }  
		  
		            }  
		        }  
	 			retMsg =  new ResponseMsg("上传失败",ErrorCode.ERROR,null);
				return retMsg;		
		        
			} catch (Exception e) {
				e.printStackTrace();
				return new ResponseMsg("上传失败",ErrorCode.SERVER_ERROR, null);
			}
  } 
    
    
    
    
    

 	 
}
